﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OPLWeb.Models;

namespace OPLWeb.Controllers
{
    public class ResultsController : Controller
    {
        private ORITAEntities db = new ORITAEntities();

        //
        // GET: /Results/

        public ViewResult Index(int Id)
        {
            MatchSummary wrapper = new MatchSummary();
            var fixture = db.Fixtures.Where(p => p.Id == Id).FirstOrDefault();
            ViewBag.CurrentSeason = fixture.SeasonId;
            ViewBag.CurrentFixture = Id;
            wrapper.ThisFixture = fixture;
            if (fixture.TeamWon.HasValue)
            {
                wrapper.Team1 = new ResultTeam();
                wrapper.Team1.Players = (from c in db.Players
                                         join o in db.PlayerTeamSeasons on c.ID equals o.PlayerId
                                         where o.TeamId == fixture.Team.ID && o.FixtureId == Id
                                         select c).ToList();
                wrapper.Team1.TeamName = fixture.Team.TeamName;
                wrapper.Team1.Batting = (from c in db.PlayerTeamSeasons
                                         where c.TeamId == fixture.Team.ID && c.BallsFaced.HasValue && c.FixtureId == Id
                                         select c).ToList();
                wrapper.Team1.Bowling = (from c in db.PlayerTeamSeasons
                                         where c.TeamId == fixture.Team.ID && c.BallsBowled.HasValue && c.FixtureId == Id
                                         select c).ToList();
                wrapper.Team1.FixtureId = fixture.Id;

                wrapper.Team2 = new ResultTeam();
                wrapper.Team2.Players = (from c in db.Players
                                         join o in db.PlayerTeamSeasons on c.ID equals o.PlayerId
                                         where o.TeamId == fixture.Team1.ID && o.FixtureId == Id
                                         select c).ToList();
                wrapper.Team2.TeamName = fixture.Team1.TeamName;
                wrapper.Team2.Batting = (from c in db.PlayerTeamSeasons
                                         where c.TeamId == fixture.Team1.ID && c.BallsFaced.HasValue && c.FixtureId == Id
                                         select c).ToList();
                wrapper.Team2.Bowling = (from c in db.PlayerTeamSeasons
                                         where c.TeamId == fixture.Team1.ID && c.BallsBowled.HasValue && c.FixtureId == Id
                                         select c).ToList();
                wrapper.Team2.FixtureId = fixture.Id;
            }
            return View("Index", wrapper);
        }

        //
        // GET: /Results/Details/5

        public ViewResult Details(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            return View(playerteamseason);
        }

        //
        // GET: /Results/Create

        public ActionResult Create()
        {
            ViewBag.BowledBy = new SelectList(db.Players, "ID", "BestBowling");
            ViewBag.CaughtBy = new SelectList(db.Players, "ID", "BestBowling");
            ViewBag.PlayerId = new SelectList(db.Players, "ID", "BestBowling");
            ViewBag.RunOutBy = new SelectList(db.Players, "ID", "BestBowling");
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName");
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName");
            return View();
        }

        //
        // POST: /Results/Create

        [HttpPost]
        public ActionResult Create(PlayerTeamSeason playerteamseason)
        {
            if (ModelState.IsValid)
            {
                db.PlayerTeamSeasons.AddObject(playerteamseason);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BowledBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.BowledBy);
            ViewBag.CaughtBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.CaughtBy);
            ViewBag.PlayerId = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.PlayerId);
            ViewBag.RunOutBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.RunOutBy);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", playerteamseason.SeasonId);
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName", playerteamseason.TeamId);
            return View(playerteamseason);
        }

        //
        // GET: /Results/Edit/5

        public ActionResult Edit(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            ViewBag.BowledBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.BowledBy);
            ViewBag.CaughtBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.CaughtBy);
            ViewBag.PlayerId = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.PlayerId);
            ViewBag.RunOutBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.RunOutBy);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", playerteamseason.SeasonId);
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName", playerteamseason.TeamId);
            return View(playerteamseason);
        }

        //
        // POST: /Results/Edit/5

        [HttpPost]
        public ActionResult Edit(PlayerTeamSeason playerteamseason)
        {
            if (ModelState.IsValid)
            {
                db.PlayerTeamSeasons.Attach(playerteamseason);
                db.ObjectStateManager.ChangeObjectState(playerteamseason, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BowledBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.BowledBy);
            ViewBag.CaughtBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.CaughtBy);
            ViewBag.PlayerId = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.PlayerId);
            ViewBag.RunOutBy = new SelectList(db.Players, "ID", "BestBowling", playerteamseason.RunOutBy);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", playerteamseason.SeasonId);
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName", playerteamseason.TeamId);
            return View(playerteamseason);
        }

        //
        // GET: /Results/Delete/5

        public ActionResult Delete(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            return View(playerteamseason);
        }

        //
        // POST: /Results/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            db.PlayerTeamSeasons.DeleteObject(playerteamseason);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}